from langchain_community.tools.ainetwork import app
# pip install langchain
# pip install langchain-zhipu
# import os
# import getpass

from langchain_zhipu import ChatZhipuAI

from langchain_core.output_parsers import StrOutputParser
from langchain.prompts.chat import (ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate)

output_parser = StrOutputParser()

# llm = ChatZhipuAI(model="glm-4")
llm = ChatZhipuAI(api_key="d3708ee404327e207b2f003775e06908.X3dgRCxbkyDfEIbh"
                  , model="glm-4", temperature=0.01)  # 温度设置为0，结果随机性
template = "You are a helpful assistant that translates english to {language}"
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template = "{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)

chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
# prompt = chat_prompt.from_messages(input_language="English", output_language="Chinese", text="How are you")
# @app.post("/chat")
# async def chat(request):
chain = chat_prompt | llm | output_parser
print(chain.invoke({"text": "How are you", "language": "chinese"}))
